package com.woniuxy.yogaapp.service.imp;

import javax.annotation.Resource;

import com.woniuxy.yogaapp.dto.UserImageDto;
import com.woniuxy.yogaapp.mapper.*;
import com.woniuxy.yogaapp.pojo.*;
import org.springframework.stereotype.Service;

import com.woniuxy.yogaapp.mapper.UserMapper;
import com.woniuxy.yogaapp.mapper.UserRolesMapper;
import com.woniuxy.yogaapp.pojo.User;
import com.woniuxy.yogaapp.pojo.UserRoles;
import com.woniuxy.yogaapp.service.UserService;

import lombok.Data;

import java.util.List;

@Data
@Service
public class UserServiceImp implements UserService {
	@Resource
	private UserMapper userMapper;
	@Resource
	private UserRolesMapper userRolesMapper;
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private CoachMapper coachMapper;
    @Resource
    private YogaClubMapper yogaClubMapper;
    @Resource
    private ClubImageMapper clubImageMapper;

	// 用户注册
	@Override
	public String addUser(User user) {
		String reslut = "注册失败！";
		int row = userMapper.addUser(user);
		int uid = userMapper.findUidByAccount(user);
		UserRoles userRoles = new UserRoles(uid, 1);
		int row1 = userRolesMapper.addStudentRoles(userRoles);
		if (row > 0 && row1 > 0) {
			reslut = "注册成功！";
		}
		return reslut;
	}


	//用户登录
	@Override
	public User login(User user) {
		
		return userMapper.login(user);
	}
	//用户找回密码    保存新密码
	@Override
	public int updateNewPass(User user) {
		
		return userMapper.updateNewPass(user);
	}

	// 通过账号 查询该用户的是什么角色
	@Override
	public String findRoleByAccount(User user) {

		return userMapper.findRoleByAccount(user);
	}

    /**
     * 找寻用户头像和名字
     *
     * @param uid
     * @return
     */
    @Override
    public UserImageDto findUidImage(Integer uid) {
        //根据uid查找用户的权限
        UserRoles userRoles = userRolesMapper.findRolesByUid(uid);
        //用户对象
		UserImageDto userImageDto = new UserImageDto();
        //查询用户的昵称
        switch (userRoles.getRid()) {
			case 1:
				//管理员
				userImageDto.setUid(uid);
				userImageDto.setImage("/image/1.jpg");
				userImageDto.setName("管理员");
				break;
            case 2:
            	//学生
                Student student = new Student();
                student.setUid(uid);
                student = studentMapper.findStudentByUid(student);
				userImageDto.setUid(uid);
				userImageDto.setImage(student.getSimage());
				userImageDto.setName(student.getSnickname());
                break;
            case 3:
            	//教练
                Coach coach = new Coach();
                coach.setUid(uid);
                coach = coachMapper.findCoachByUid(coach);
				userImageDto.setUid(uid);
				userImageDto.setImage(coach.getCimage());
				userImageDto.setName(coach.getCnickname());
                break;
			case 4:
				//会馆
				YogaClub clubyUid = yogaClubMapper.findClubbyUid(uid);
				System.out.println(clubyUid);
				List<ClubImage> clubImages = clubImageMapper.findImage(clubyUid.getYid());
				userImageDto.setUid(uid);
				if(clubImages != null && clubImages.size() != 0){
					userImageDto.setImage(clubImages.get(0).getImage());
				}
				userImageDto.setName(clubyUid.getYname());
				break;
			default:
                return null;
        }
		if (userImageDto.getUid() == null || userImageDto.getUid() == 0 ) {
			return null;
		}

        return userImageDto;
    }

	/**
	 * 通过uid找用户的角色
	 * @param uid
	 * @return
	 */
	@Override
	public Integer findRoleByUid(Integer uid) {
		UserRoles rolesByUid = userRolesMapper.findRolesByUid(uid);
		return rolesByUid.getRid();
	}

}
